/***********************************************************
 * Mapping management including target, host, hostGroup
 ***********************************************************/
/**
 * 
 */
var _groupId;
var _hostId;
var _targetId;
var Target = {
	/**
	 * Show list
	 */
	list : function() {
		// Request user list url.
		var targetListUrl = baseURL + "/mapping/mainlist"
		$.ajax({
			type : "get",
			url : targetListUrl,
			dataType : "html",
			success : function(html) {
				// Request successfully and fill the data.
				$("#targetList").empty().html(html);
			}
		});
	},
	/**
	 * New target
	 */
	create : function() {
		var title = localeMsg.target.create;
		_targetId = "";
		common.dialog({
			obj : "#editTargetDialog",
			type : "get",
			url : baseURL + "/mapping/target/edit",
			title : title,
			width : 400,
			confirm : function() {
				if($("#editTargetForm").form('validate')!=true){
					return;
				}
				Target.save();
			}
		});
	},
	/**
	 * Edit target
	 */
	edit : function() {
		// Get the first targetId from checked buttons.
		
		var targetIds =[];
		$("input[name='targetId']:checked").each(function(){
			targetIds.push($(this).val()); 
            });
		if(!(targetIds.length>0)) {
			return;
		}
		// The title of edit dialog
		var title = localeMsg.target.create;
		var param = {};
		// If id is not empty, edit the target info; or new it.
		if(targetIds.length==0){
			common.showTip({
				title : localeMsg.common.warn ,
				text : localeMsg.common.selectMsg
			});
		}else if(targetIds.length>1){
			common.showTip({
				title : localeMsg.common.warn ,
				text : localeMsg.common.selectMsg
			});
		}else{
			var targetId = $("input[name='targetId']:checked").eq(0).val();
			_targetId = targetId;
			title = localeMsg.target.edit;
			param.targetId = targetId;
			common.dialog({
				obj : "#editTargetDialog",
				type : "get",
				url : baseURL + "/mapping/target/edit",
				param : param,
				title : title,
				width : 400,
				confirm : function() {
					if($("#editTargetForm").form('validate')!=true){
						return;
					}
					Target.save();
				}
			});
		}
	},
	/**
	 * Save pool
	 */
	save : function() {
		$("#editTargetForm").ajaxSubmit({
			success : function(json) {
				// Save successfully and refresh the user list.
				if (json.result && json.result == "success") {
					common.showTip({
						title : localeMsg.common.prompt,
						text : localeMsg.common.saveSuccess,
						success : true,
						callback : function() {
							Target.list();
							//main.list();
						}
					});
				} else {
					common.showTip({title : localeMsg.common.warn, text : localeMsg.common.saveFailed});
				}
			}
		});
	},
	/**
	 * Remove targets
	 */
	remove : function() {
		var targetIds=[];
		$('input[name="targetId"]:checked').each(function(){
			targetIds.push($(this).val()); 
		});
		if(!(targetIds.length>0)) {
			return;
		}
		if(targetIds==""){
			common.showTip({title : localeMsg.common.warn, text : localeMsg.common.selectMsg});
		}else{
			common.confirmDialog(localeMsg.common.deleteConfirm, function() {
				$.ajax({
					type : "post",
					url : baseURL + "/mapping/target/remove",
					data : {targetIds : targetIds},
					traditional: true,
					dataType : "json",
					success : function(json) {
						if (json.result && json.result == "success") {
							common.showTip({
								title : localeMsg.common.prompt,
								text : localeMsg.common.deleteSuccess,
								success : true,
								callback : function() {
									Target.list();
								}
							});
						} 
						else if(json.result && json.result == "failure")
							common.showTip({title : localeMsg.common.warn, text : "该控制器还包含映射，不能删除"});
						else {
							common.showTip({title : localeMsg.common.warn, text : localeMsg.common.deleteFail});
						}
					}
				});
			});
		}
		
	},
	
	validate : function(){
		$('#targetName').validatebox({
			required : true,
			validType :	['vername','uniquename["/mapping/target/validateName","'+_targetId+'"]']
		});
		$('#targetIp').validatebox({
			required : true,
			validType : ['ip','uniqueip["/mapping/target/validateIp","'+_targetId+'"]']
		});
	},
	
	/**
	 * 显示hg列表
	 * @param element
	 * @param type 显示类型
	 */
	showGroups : function(element, type) {
		var confirmText = localeMsg.common.confirm
		var targetId = $(element).parent().siblings().find("input[name='targetId']").val();
		var url = baseURL + "/mapping/hg/listNoAction";
		var param = {targetId : targetId};
		if (type) {
			param.type = type;
		} else {
			confirmText = localeMsg.common.deleted;
		}
		var dialog;
		if ($("#groupList").length < 1) {
			$("#listGroupForm").append('<div id="groupList" ></div>');
			dialog = "#groupList";
		}
		common.dialog({
			obj : dialog,
			title : localeMsg.group.list,
			width : 680,
			confirm : function(dialog) {
				var groupIdArr = [];
				$(dialog).find("input[name='groupId']:checked").each(function() {
					groupIdArr.push($(this).val());
				});
				if(!(groupIdArr.length>0)) {
					return;
				}
				Target.saveTargetGroups(targetId, groupIdArr,type);
			},
			confirmText : confirmText,
			onOpen : function() {
				$("#listGroupForm").ajaxpager({
					target: "#groupList",
					callback: function() {
						Target.showGroupsByPager(targetId, type);
					}
				});
			}
		});
	},
	
	showGroupsByPager : function(targetId, type) {
		var pageNumber = $("#groupList input[name='pageNumber']").val();
		var url = baseURL + "/mapping/hg/listNoAction";
		var param = {targetId : targetId, pageNumber : pageNumber};
		if (type) {
			param.type = type;
		}
		$.ajax({
			type : "get",
			url : url,
			data : param,
			success : function(html) {
				$("#groupList").empty().html(html);
				$("#groupList").dialog("resize");
			}
		});
	},
	/**
	 * 保存target-hostGroup关系
	 * @param targetId
	 * @param groupIdArr
	 */
	saveTargetGroups : function(targetId, groupIdArr,type) {
		$.ajax({
			type : "post",
			url : baseURL + "/mapping/target/hg",
			traditional: true,
			data : {targetId: targetId, hgIds : groupIdArr,type :type},
			dataType : "json",
			success : function(json) {
				common.showTip({
					title : localeMsg.common.prompt,
					text : localeMsg.common.saveSuccess,
					success : true
				});
			}
		});
	},
};

/**
 * 
 */
var Group = {
	/**
	 * Show group list
	 */
	list : function() {
		// Request user list url.
		var groupListUrl = baseURL + "/mapping/hg/list"
		$.ajax({
			type : "get",
			url : groupListUrl,
			dataType : "html",
			success : function(html) {
				// Request successfully and fill the data.
				$("#hgList").empty().html(html);
			}
		});
	},
	/**
	 * New group
	 */
	create : function() {
		var title = localeMsg.group.create;
		_groupId = "";
		common.dialog({
			obj : "#editGroupDialog",
			type : "get",
			url : baseURL + "/mapping/hg/edit",
			title : title,
			width : 400,
			confirm : function() {
				if($("#editGroupForm").form('validate')!=true){
					return;
				}
				Group.save();
			}
		});
	},
	/**
	 * Edit group
	 */
	edit : function() {
		// Get the first groupId from checked buttons.
		var groupIds = [];
		$("input[name='groupId']:checked").each(function(){
			groupIds.push($(this).val()); 
        });
		if(!(groupIds.length>0)) {
			return;
		}
		// The title of edit dialog
		var title = localeMsg.group.create;
		var param = {};
		if(groupIds.length==0){
			common.showTip({
				title : localeMsg.common.warn ,
				text : localeMsg.common.selectMsg
			});
		}else if(groupIds.length>1){
			common.showTip({
				title : localeMsg.common.warn ,
				text : localeMsg.common.selectMsg
			});
		}else{
			var groupId = $("input[name='groupId']:checked").eq(0).val();
			_groupId = groupId;
			title = localeMsg.group.edit;
			param.groupId = groupId;
			common.dialog({
				obj : "#editHgDialog",
				type : "get",
				url : baseURL + "/mapping/hg/edit",
				param : param,
				title : title,
				width : 400,
				confirm : function() {
					Group.save();
				}
			});
		}
	},
	/**
	 * Save group
	 */
	save : function() {
		var groupId = $("#editGroupForm input[id='id']").eq(0).val();
		var groupName = $("#editGroupForm input[id='groupName']").eq(0).val();
		var chap= $("input:radio[name='chap']:checked").val();
		var inuser=$("#incoming_user").val();
		var inpassword=$("#incoming_password").val();
		var outuser=$("#outgoing_user").val();
		var outpassword=$("#outgoing_password").val();
		
		var saveUrl = baseURL + "/mapping/hg/save";
		$.ajax({
			type : "post",
			url : saveUrl,
			data : {
					groupId : groupId , 
					groupName : groupName,
					chap:chap,
					inuser:inuser,
					inpassword:inpassword,
					outuser:outuser,
					outpassword:outpassword
					},
			dataType : "json",
			success : function(json) {
				if(json.data && json.data == "0"){
					common.showTip({
						title : localeMsg.common.prompt,
						text : localeMsg.common.exit,
				});
				}else{
					if (json.result && json.result == "success") {
						common.showTip({
							title : localeMsg.common.prompt,
							text : localeMsg.common.saveSuccess,
							//对错的提示图标
							success : true,
							callback : function() {
								Group.list();
							}
						});
					} else {
						common.showTip({title : localeMsg.common.warn, text : localeMsg.common.saveFail});
					}
				}
			}
		})
	},
	
	
	
	
	/**
	 * Remove groups confirm
	 */
	remove : function(){
		var roleId=[];
		$('input[name="groupId"]:checked').each(function(){
			roleId.push($(this).val()); 
            });  
		if (Object.keys(roleId).length === 0){
			common.showTip({
				title : localeMsg.common.warn,
				text : localeMsg.common.selectMsg
			});
		}else{
			var confirmMessage = localeMsg.common.relateddeleteconfirm
				common.confirmDialog(confirmMessage,Group.removeDel);
		}

	},
	
	
	/**
	 * Remove groups
	 */
	removeDel : function() {
		//  Remove  records
		var groupId=[];
		$('input[name="groupId"]:checked').each(function(){
			groupId.push($(this).val()); 
        });  
		if(!(groupId.length>0)) {
			return;
		}
		$.ajax({
			type : "post",
			url : baseURL + "/mapping/hg/remove",
			data : {groupIds : groupId},
			traditional: true,
			dataType : "json",
			success : function(json) {
				if (json.result && json.result == "success") {
					common.showTip({
						title : localeMsg.common.prompt,
						text : localeMsg.common.deleteSuccess,
						success : true,
						callback : function() {
							Group.list();
						}
					});
				} else if(json.result && json.result == "HASLUN"){
					common.showTip({title : localeMsg.common.warn, text : localeMsg.group.haslun});
				}
				else {
					common.showTip({title : localeMsg.common.warn, text : localeMsg.common.deleteFailed});
				}
			}
		});
	},
	
	validate : function(){
	    $('#groupName').validatebox({
	        required: true,
	        validType :	['vername','uniquename["/mapping/hg/validateName","'+_groupId+'"]']
	    });
	},
	
	checkInput : function(){
	    var value = $("input:radio[name='chap']:checked").val();
		if(value==1){
			$('#incoming_user').validatebox({
		        required: true,
		        validType :	['vername']
			});
			$('#incoming_password').validatebox({
		        required: true,
			});
			$('#outgoing_user').validatebox({
		        required: true,
		        validType :	['vername'],
		        novalidate : true
			});
			$('#outgoing_password').validatebox({
		        required: true,
		        novalidate : true
			});
		} else if(value==2){
			$('#incoming_user').validatebox({
		        required: true,
		        validType :	['vername'],
		        novalidate : false
			});
			$('#incoming_password').validatebox({
		        required: true,
		        novalidate : false
			});
			$('#outgoing_user').validatebox({
		        required: true,
		        validType :	['vername'],
		        novalidate : false
			});
			$('#outgoing_password').validatebox({
		        required: true,
		        novalidate : false
			});
		} else {
			$('#incoming_user').validatebox({novalidate : true});
			$('#incoming_password').validatebox({novalidate : true});
			$('#outgoing_user').validatebox({novalidate : true});
			$('#outgoing_user').validatebox({novalidate : true});
		}
	},
	

	/**
	 * 显示target选项
	 * @param e
	 */
	showTargets : function(element) {
		$.ajax({
			type : "get",
			url : baseURL + "/mapping/target/list.json",
			dataType : "json",
			success : function(json) {
				for (var k in json) {
					$(element).append('<option value="' + json[k].id + '">' + json[k].name + '</option>');
				}
			}
		});
	},
	
	/**
	 * 显示host列表
	 * @param element
	 * @param type 显示类型
	 */
	showHosts : function(element, type) {
		var confirmText = localeMsg.common.confirm
		var groupId = $(element).parent().siblings().find("input[name='groupId']").val();
		var title = localeMsg.host.list;
		var url = baseURL + "/mapping/host/list";
		var param = {groupId : groupId};
		if (type) {
			param.type = type;
			title = localeMsg.host.add;
		} else {
			confirmText = localeMsg.common.deleted;
		}
		var dialog;
		if ($("#hostList").length < 1) {
			$("#listHostForm").append('<div id="hostList"></div>');
			dialog = "#hostList";
		}
		common.dialog({
			obj : dialog,
			title : title,
			width : 600,
			height : 540,
			confirm : function(dialog) {
				var hostIdArr = [];
				$(dialog).find("input[name='hostId']:checked").each(function() {
					hostIdArr.push($(this).val());
				});
				if(!(hostIdArr.length>0)) {
					return;
				}
				Group.saveGroupHosts(groupId, hostIdArr,type);
			},
			confirmText : confirmText,
			onOpen : function() {
				$("#listHostForm").ajaxpager({
					target: "#hostList",
					callback: function() {
						Group.showHostsByPager(groupId, type);
					}
				});
			}
		});
	},
	showHostsByPager : function(groupId, type) {
		var pageNumber = $("#hostList input[name='pageNumber']").val();
		var url = baseURL + "/mapping/host/list";
		var param = {groupId : groupId, pageNumber : pageNumber};
		if (type) {
			param.type = type;
		}
		$.ajax({
			type : "get",
			url : url,
			data : param,
			success : function(html) {
				$("#hostList").empty().html(html);
				//$("#hostList").dialog("resize");
			}
		});
	},
	/**
	 * 保存group-host关系
	 * @param groupId
	 * @param hostIdArr
	 */
	saveGroupHosts : function(groupId, hostIdArr,type) {
		$.ajax({
			type : "post",
			url : baseURL + "/mapping/hg/hosts",
			traditional: true,
			data : {groupId: groupId, hostIds : hostIdArr, type : type},
			dataType : "json",
			success : function(json) {
				if (json.result && json.result == "success") {
					common.showTip({
						title : localeMsg.common.prompt,
						text : localeMsg.common.saveSuccess,
						success: true
					});
				}else if(json.result && json.result == "wrong"){
					var names = "";
					for(var i=0; i<json.hostNames.length; i++)  
                       {  
                        names+= json.hostNames[i]+",";
                       }  
					names = names.substr(0,names.length - 1);
					common.showTip({
						title : localeMsg.common.warn,
						text : names+localeMsg.group.not+json.lunName+localeMsg.group.mapping,
						success: false
					});
				}else {
					common.showTip({
						title : localeMsg.common.warn,
						text : localeMsg.common.saveFailed,
						success: false
					});
				}
				
				
				/*common.showTip({
					title : localeMsg.common.prompt,
					text : localeMsg.common.saveSuccess,
					success : true
				});*/
			}
		});
	},
	/**
	 * 显示未与指定hostgroup做映射的lun列表
	 * @param element
	 */
	
	/*showLuns : function(element) {
		var confirmText = localeMsg.common.confirm;
		var groupId = $("#hgList input[name='groupId']:checked").val();
		var title = localeMsg.lun.list;
		var url = baseURL + "/mapping/luns";
		var param = {groupId : groupId};
		if (type) {
			param.type = type;
			title = localeMsg.host.add;
		} else {
			confirmText = localeMsg.common.deleted;
		}
		var dialog;
		if ($("#lunList").length < 1) {
			$("#listLunForm").append('<div id="lunList"></div>');
			dialog = "#lunList";
		}
		if(groupId == undefined){
			common.showTip({
				title : localeMsg.common.prompt,
				text : localeMsg.common.selectMsg
			});
		}else{
			common.dialog({
				obj : dialog,
				title : title,
				width : 600,
				confirm : function(dialog) {
					var lunIdArr = [];
					$(dialog).find("input[name='lunId']:checked").each(function() {
						lunIdArr.push($(this).val());
					});
					if(!(lunIdArr.length>0)) {
						return;
					}
					Group.mapping(groupId, lunIdArr);
				},
				confirmText : confirmText,
				onOpen : function() {
					$("#listLunForm").ajaxpager({
						target: "#lunList",
						callback: function() {
							Group.showlunsByPager(groupId);
						}
					});
				}
			});
		}
	},
	*/
	showLuns : function(element) {
		var confirmText = localeMsg.common.confirm;
		var groupId = $("#hgList input[name='groupId']:checked").val();
		var title = localeMsg.lun.list;
		var url = baseURL + "/mapping/luns";
		var param = {groupId : groupId};
		var groupIds =[];
		$("input[name='groupId']:checked").each(function(){
			groupIds.push($(this).val()); 
            });
		if(!(groupIds.length>0)) {
			return;
		};
		var dialog;
		if ($("#lunList").length < 1) {
			$("#listLunForm").append('<div id="lunList"></div>');
			dialog = "#lunList";
		}
		if(groupId == undefined){
			common.showTip({
				title : localeMsg.common.prompt,
				text : localeMsg.common.selectMsg
			});
		}else if(groupIds.length>1){
			common.showTip({
				title : localeMsg.common.warn ,
				text : localeMsg.common.selectMsg
			});
		}else{
			common.dialog({
				obj : dialog,
				title : title,
				width : 600,
				height : 540,
				confirm : function(dialog) {
					var lunIdArr = [];
					$(dialog).find("input[name='lunId']:checked").each(function() {
						lunIdArr.push($(this).val());
					});
					if(!(lunIdArr.length>0)) {
						return;
					}
					Group.mapping(groupId, lunIdArr);
				},
				confirmText : confirmText,
				onOpen : function() {
					$("#listLunForm").ajaxpager({
						target: "#lunList",
						callback: function() {
							Group.showlunsByPager(groupId);
						}
					});
				}
			});
		}
	},
	showlunsByPager : function(groupId) {
		var pageNumber = $("#lunList input[name='pageNumber']").val();
		var url = baseURL + "/mapping/luns";
		var param = {groupId : groupId, pageNumber : pageNumber};
		
		$.ajax({
			type : "get",
			url : url,
			data : param,
			success : function(html) {
				$("#lunList").empty().html(html);
				$("#lunList").dialog("resize");
			}
		});
	},
	/*
	showHosts : function(element, type) {
		var confirmText = localeMsg.common.confirm
		var groupId = $(element).parent().siblings().find("input[name='groupId']").val();
		var title = localeMsg.host.list;
		var url = baseURL + "/mapping/host/list";
		var param = {groupId : groupId};
		if (type) {
			param.type = type;
			title = localeMsg.host.add;
		} else {
			confirmText = localeMsg.common.deleted;
		}
		var dialog;
		if ($("#hostList").length < 1) {
			$("#listHostForm").append('<div id="hostList"></div>');
			dialog = "#hostList";
		}
		common.dialog({
			obj : dialog,
			title : title,
			width : 600,
			confirm : function(dialog) {
				var hostIdArr = [];
				$(dialog).find("input[name='hostId']:checked").each(function() {
					hostIdArr.push($(this).val());
				});
				if(!(hostIdArr.length>0)) {
					return;
				}
				Group.saveGroupHosts(groupId, hostIdArr,type);
			},
			confirmText : confirmText,
			onOpen : function() {
				$("#listHostForm").ajaxpager({
					target: "#hostList",
					callback: function() {
						Group.showHostsByPager(groupId, type);
					}
				});
			}
		});
	},
	showHostsByPager : function(groupId, type) {
		var pageNumber = $("#hostList input[name='pageNumber']").val();
		var url = baseURL + "/mapping/host/list";
		var param = {groupId : groupId, pageNumber : pageNumber};
		if (type) {
			param.type = type;
		}
		$.ajax({
			type : "get",
			url : url,
			data : param,
			success : function(html) {
				$("#hostList").empty().html(html);
				//$("#hostList").dialog("resize");
			}
		});
	},*/
	/**
	 * 查看映射信息
	 * @param element
	 */

	showMappings : function(element) {
		var confirmText = localeMsg.common.confirm;
		var groupId = $("#hgList input[name='groupId']:checked").val();
		var title = localeMsg.lun.list;
		var url = baseURL + "/mapping/list";
		var param = {groupId : groupId};
		var groupIds =[];
		$("input[name='groupId']:checked").each(function(){
			groupIds.push($(this).val()); 
            });
		if(!(groupIds.length>0)) {
			return;
		};
		var dialog;
		if ($("#mappedLunList").length < 1) {
			$("#listMappedLunForm").append('<div id="mappedLunList"></div>');
			dialog = "#mappedLunList";
		}
		if(groupId == undefined){
			common.showTip({
				title : localeMsg.common.prompt,
				text : localeMsg.common.selectMsg
			});
		}else if(groupIds.length>1){
			common.showTip({
				title : localeMsg.common.warn ,
				text : localeMsg.common.selectMsg
			});
		}else{
			common.dialog({
				obj : dialog,
				title : title,
				width : 600,
				height : 540,
				confirm : function(dialog) {
					var lunIdArr = [];
					$(dialog).find("input[name='lunId']:checked").each(function() {
						lunIdArr.push($(this).val());
					});
					if(!(lunIdArr.length>0)) {
						return;
					}
					Group.unmapping(groupId, lunIdArr);
				},
				confirmText : confirmText,
				onOpen : function() {
					$("#listMappedLunForm").ajaxpager({
						target: "#mappedLunList",
						callback: function() {
							Group.showMappedlunsByPager(groupId);
						}
					});
				}
			});
		}
	},
	showMappedlunsByPager : function(groupId) {
		var pageNumber = $("#mappedLunList input[name='pageNumber']").val();
		var url = baseURL + "/mapping/list";
		var param = {groupId : groupId, pageNumber : pageNumber};
		
		$.ajax({
			type : "get",
			url : url,
			data : param,
			success : function(html) {
				$("#mappedLunList").empty().html(html);
				$("#mappedLunList").dialog("resize");
			}
		});
	},
	
	/**
	 * 映射hostGroup-lun
	 * @param groupId
	 * @param lunArr
	 */
	mapping : function(groupId, lunArr) {
		$.ajax({
			type : "post",
			url : baseURL + "/mapping",
			traditional: true,
			data : {groupId: groupId, lunIds : lunArr},
			dataType : "json",
			success : function(json) {
				if (json.result && json.result == "success") {
					common.showTip({
						title : localeMsg.common.prompt,
						text : localeMsg.common.saveSuccess,
						success: true
					});
				}else if(json.result && json.result == "wrong"){
					common.showTip({
						title : localeMsg.common.warn,
						text : localeMsg.lun.acl,
						success: false
					});
				}else {
					common.showTip({
						title : localeMsg.common.warn,
						text : localeMsg.common.saveFailed,
						success: false
					});
				}
				
				/*common.showTip({
					title : localeMsg.common.prompt,
					text : localeMsg.common.saveSuccess,
					success : true
				});*/
			}
		});
	},
	/**
	 * 解除映射
	 * @param groupId
	 * @param lunArr
	 */
	unmapping : function(groupId, lunArr) {
		$.ajax({
			type : "post",
			url : baseURL + "/mapping/delete",
			traditional: true,
			data : {groupId: groupId, ids : lunArr},
			dataType : "json",
			success : function(json) {
				common.showTip({
					title : localeMsg.common.prompt,
					text : localeMsg.common.saveSuccess,
					success : true
				});
			}
		});
	},
}

/**
 * 
 */
var Host = {
	/**
	 * Show host list
	 */
	list : function() {
		// Request host list url.
		var hostListUrl = baseURL + "/mapping/host/list"
		$.ajax({
			type : "get",
			url : hostListUrl,
			dataType : "html",
			success : function(html) {
				$("#hostList").empty().html(html);
			}
		});
	},
	/**
	 * New host
	 */
	create : function() {
		var title = localeMsg.host.create;
		_hostId = "";
		common.dialog({
			obj : "#editHostDialog",
			type : "get",
			url : baseURL + "/mapping/host/edit",
			/*param : param,*/
			title : title,
			width : 400,
			confirm : function() {
				if($("#editHostForm").form('validate')!=true){
					return;
				}
				Host.save();
			}
		});
	},
	/**
	 *Edit host
	 */
	edit : function() {
		var hostIds =[];
		$("input[name='hostId']:checked").each(function(){
			hostIds.push($(this).val()); 
            });
		if(!(hostIds.length>0)) {
			return;
		}
		var title = localeMsg.host.create;
		var param = {};
		if(hostIds.length==0){
			common.showTip({
				title : localeMsg.common.warn ,
				text : localeMsg.common.selectMsg
			});
		}else if(hostIds.length>1){
			common.showTip({
				title : localeMsg.common.warn ,
				text : localeMsg.common.selectMsg
			});
		}else{
			var hostId = $("input[name='hostId']:checked").eq(0).val();
			_hostId = hostId;
			title = localeMsg.host.edit;
			param.hostId = hostId;
			common.dialog({
				//obj : "#editHostDialog",
				type : "get",
				url : baseURL + "/mapping/host/edit",
				param : param,
				title : title,
				width : 400,
				confirm : function() {
					if($("#editHostForm").form('validate')!=true){
						return;
					}
					Host.save();
				}
			});
		}
	},
	/**
	 * Save host
	 */
	save : function() {
		$("#editHostForm").ajaxSubmit({
			success : function(json) {
				if (json.result && json.result == "success") {
					common.showTip({
						title : localeMsg.common.prompt,
						text : localeMsg.common.saveSuccess ,
						success : true,
						callback : function() {
							Host.list();
						}
					});
				} else {
					common.showTip({title : localeMsg.common.warn, text : localeMsg.common.saveFailed});
				}
			}
		});
	},
	
	
	
	
	remove : function(){
		var hostId=[];
		$('input[name="hostId"]:checked').each(function(){
			hostId.push($(this).val()); 
            });  
		if (Object.keys(hostId).length === 0){
			common.showTip({
				title : localeMsg.common.warn,
				text : localeMsg.common.selectMsg 
			});
		}else{
			var confirmMessage = localeMsg.common.deleteConfirm
				common.confirmDialog(confirmMessage,Host.removeDel);
		}

	},
	/**
	 * Remove hosts
	 */
	removeDel : function() {
	
			var hostIds=[];
			$('input[name="hostId"]:checked').each(function(){
				hostIds.push($(this).val()); 
			});  
			if(!(hostIds.length>0)) {
				return;
			}
			
			$.ajax({
				type : "post",
				url : baseURL + "/mapping/host/remove",
				data : {hostIds : hostIds},
				traditional: true,
				dataType : "json",
				success : function(json) {
					if (json.result && json.result == "success") {
						common.showTip({
							title : localeMsg.common.prompt,
							text : localeMsg.common.deleteSuccess ,
							success : true,
							callback : function() {
								Host.list();
							}
						});
					}else if(json.result && json.result == "wrong"){
						common.showTip({
							title : localeMsg.common.prompt,
							text : localeMsg.host.notdelete,
							success : false,
						});
					}
					else {
						common.showTip({title : localeMsg.common.warn, text : localeMsg.common.deleteFail});
					}
				}
			});	
	},
	validate : function(){
		$('#hostName').validatebox({
			required : true,
			validType :	['vername','uniquename["/mapping/host/validateName", "'+_hostId+'"]']
		});
		$('#ipAddress').validatebox({
			required : true,
			validType : ['ip','uniqueip["/mapping/host/validateIp", "'+_hostId+'"]']
		});
	},
};

var main = {
		/**
		 * Show list
		 */
		list : function() {
			// Request user list url.
			var targetListUrl = baseURL + "/mapping/main/list"
			$.ajax({
				type : "get",
				url : targetListUrl,
				dataType : "html",
				success : function(html) {
					// Request successfully and fill the data.
					$("#targetList").empty().html(html);
				}
			});
		},
		
		/**
		 * 显示hg列表
		 * @param element
		 * @param type 显示类型
		 */
		showGroups : function(value,name, type) {
			var targetId = value;
			var url = baseURL + "/mapping/hostgroupList";
			
			$.ajax({
				type : "post",
				url : url,
				data : {targetId: targetId,name : name ,type :type},
				dataType : "html",
				success : function(html) {
					$("#mastergroup").empty().html(html);
					$("#masterhost").empty();
				}
			});
			
		},
		
	
		/**
		 * 保存target-hostGroup关系
		 * @param targetId
		 * @param groupIdArr
		 */
		saveTargetGroups : function(targetId,type) {
			var groupIdArr = [];
			$("input[name='groupId']:checked").each(function() {
				groupIdArr.push($(this).val());
			});
			if(groupIdArr.length >0){
				$.ajax({
					type : "post",
					url : baseURL + "/mapping/target/hg",
					traditional: true,
					data : {targetId: targetId, hgIds : groupIdArr,type :type},
					dataType : "json",
					success : function(json) {
							main.showGroups(targetId);
							common.showTip({
								title : localeMsg.common.prompt,
								text : localeMsg.common.saveSuccess,
								success : true
							});
					}
				});
			}else{
				common.showTip({
					title : localeMsg.common.warn,
					text : localeMsg.common.selectMsg
				});
			}

		},
		
		
		/**
		 * 显示hg列表
		 * @param element
		 * @param type 显示类型
		 */
		addGroups : function(element, type) {
			var confirmText = localeMsg.common.confirm
			var targetId = element;
			var url = baseURL + "/mapping/hg/listNoAction";
			var param = {targetId : targetId};
			if (type) {
				param.type = type;
			} else {
				confirmText = localeMsg.common.deleted;
			}
			var dialog;
			if ($("#groupList").length < 1) {
				$("#listGroupForm").append('<div id="groupList" ></div>');
				dialog = "#groupList";
			}
			common.dialog({
				obj : dialog,
				title : localeMsg.group.list,
				width : 680,
				confirm : function(dialog) {
					var groupIdArr = [];
					$(dialog).find("input[name='groupId']:checked").each(function() {
						groupIdArr.push($(this).val());
					});
					if(!(groupIdArr.length>0)) {
						return;
					}
					main.saveTargetGroups(targetId,type);
				},
				confirmText : confirmText,
				onOpen : function() {
					$("#listGroupForm").ajaxpager({
						target: "#groupList",
						callback: function() {
							Target.showGroupsByPager(targetId, type);
						}
					});
				}
			});
		},
		
		/**
		 * 显示host列表
		 * @param element
		 * @param type 显示类型
		 */
		addHosts : function(element, type) {
			var confirmText = localeMsg.common.confirm
			var groupId = element;
			var title = localeMsg.host.list;
			var url = baseURL + "/mapping/host/list";
			var param = {groupId : groupId};
			if (type) {
				param.type = type;
				title = localeMsg.host.add;
			} else {
				confirmText = localeMsg.common.deleted;
			}
			var dialog;
			if ($("#hostList").length < 1) {
				$("#listHostForm").append('<div id="hostList"></div>');
				dialog = "#hostList";
			}
			common.dialog({
				obj : dialog,
				title : title,
				width : 600,
				height : 540,
				confirm : function(dialog) {
					var hostIdArr = [];
					$(dialog).find("input[name='hostId']:checked").each(function() {
						hostIdArr.push($(this).val());
					});
					if(!(hostIdArr.length>0)) {
						return;
					}
					main.saveGroupHosts(groupId,type);
				},
				confirmText : confirmText,
				onOpen : function() {
					$("#listHostForm").ajaxpager({
						target: "#hostList",
						callback: function() {
							Group.showHostsByPager(groupId, type);
						}
					});
				}
			});
		},
		
		/**
		 * 显示host列表
		 * @param element
		 */
		showHosts : function(groupId, groupName) {
			var url = baseURL + "/mapping/hostList";
			var param = {groupId : groupId};
			var dialog;
			
			$.ajax({
				type : "post",
				url : url,
				data : {groupId: groupId},
				dataType : "html",
				success : function(html) {
					$("#masterhost").empty().html(html);
					$("#hostGroupName").text(groupName);
				}
			});
		},
		
		
		deleteGroupHosts : function(groupId){
			var hostIdArr = [];
			$("input[name='hostId']:checked").each(function() {
				hostIdArr.push($(this).val());
			});
			if(hostIdArr.length ==0){
				common.showTip({
					title : localeMsg.common.warn,
					text : localeMsg.common.selectMsg
				});
			}else{
				var confirmMessage = localeMsg.host.relateddeleteconfirm;
				common.confirmDialog(confirmMessage, function() {
					main.saveGroupHosts(groupId);
				});

			}
		},
		
		/**
		 * 保存group-host关系
		 * @param groupId
		 * @param hostIdArr
		 */
		saveGroupHosts : function(groupId,type) {
			var hostIdArr = [];
			$("input[name='hostId']:checked").each(function() {
				hostIdArr.push($(this).val());
			});
			if(hostIdArr.length >0){
				$.ajax({
					type : "post",
					url : baseURL + "/mapping/hg/hosts",
					traditional: true,
					data : {groupId: groupId, hostIds : hostIdArr, type : type},
					dataType : "json",
					success : function(json) {
						if (json.result && json.result == "success") {
							common.showTip({
								title : localeMsg.common.prompt,
								text : localeMsg.common.saveSuccess,
								success: true,
								callback: function() { main.showHosts(groupId); }
							});
						}else if(json.result && json.result == "wrong"){
							var names = "";
							for(var i=0; i<json.hostNames.length; i++)  
		                       {  
		                        names+= json.hostNames[i]+",";
		                       }  
							names = names.substr(0,names.length - 1);
							common.showTip({
								title : localeMsg.common.warn,
								text : names+localeMsg.group.not+json.lunName+localeMsg.group.mapping,
								success: false
							});
						}
						else {
							common.showTip({
								title : localeMsg.common.warn,
								text : localeMsg.common.saveFailed,
								success: false
							});
						}
					}
				});
			}else{
				common.showTip({
					title : localeMsg.common.warn,
					text : localeMsg.common.selectMsg
				});
			}

		},
		
		/**
		 * 显示未与指定hostgroup做映射的lun列表
		 * @param element
		 */
		
		showLuns : function(value) {
			var confirmText = localeMsg.common.confirm;
			var groupId = value;
			var title = localeMsg.lun.list;
			var url = baseURL + "/mapping/luns";
			var param = {groupId : groupId};
			/*if (type) {
				param.type = type;
				title = localeMsg.host.add;
			} else {
				confirmText = localeMsg.common.deleted;
			}*/
			var dialog;
			if ($("#lunList").length < 1) {
				$("#listLunForm").append('<div id="lunList"></div>');
				dialog = "#lunList";
			}
			if(groupId == undefined){
				common.showTip({
					title : localeMsg.common.prompt,
					text : localeMsg.common.selectMsg
				});
			}else{
				common.dialog({
					obj : dialog,
					title : title,
					width : 600,
					height : 540,
					confirm : function(dialog) {
						var lunIdArr = [];
						$(dialog).find("input[name='lunId']:checked").each(function() {
							lunIdArr.push($(this).val());
						});
						if(!(lunIdArr.length>0)) {
							return;
						}
						main.mapping(groupId, lunIdArr);
					},
					confirmText : confirmText,
					onOpen : function() {
						$("#listLunForm").ajaxpager({
							target: "#lunList",
							callback: function() {
								Group.showlunsByPager(groupId);
							}
						});
					}
				});
			}
		},
		
		/**
		 * 查看映射信息
		 * @param element
		 */
		showMappings : function(groupId, groupName,type) {
			var confirmText = localeMsg.common.confirm;
			var title = localeMsg.lun.list;
			var url = baseURL + "/mapping/list";
			if( typeof(type)=="undefined")
				type="";
			_groupId = groupId;
			$.ajax({
				type : "get",
				url : url,
				data : {groupId: groupId,type:type},
				dataType : "html",
				success : function(html) {
					$("#masterhost").empty().html(html);
					$("#hostGroupName").text(groupName);
				}
			});
		},
		
		/**
		 * 映射hostGroup-lun
		 * @param groupId
		 * @param lunArr
		 */
		mapping : function(groupId, lunArr) {
			$.ajax({
				type : "post",
				url : baseURL + "/mapping",
				traditional: true,
				data : {groupId: groupId, lunIds : lunArr},
				dataType : "json",
				success : function(json) {
					if (json.result && json.result == "success") {
						common.showTip({
							title : localeMsg.common.prompt,
							text : localeMsg.common.saveSuccess,
							success: true,
							callback: function(){ main.showMappings(groupId); }
						});
					}else if(json.result && json.result == "wrong"){
						common.showTip({
							title : localeMsg.common.warn,
							text : localeMsg.lun.acl,
							success: false
						});
					}else {
						common.showTip({
							title : localeMsg.common.warn,
							text : localeMsg.common.saveFailed,
							success: false
						});
					}
				}
			});
		},
		/*
		 * 
		 * */
		midunmapping : function(){
			var lunIdArr = [];
			$("input[name='lunId']:checked").each(function() {
				lunIdArr.push($(this).val());
			});
			if(!(lunIdArr.length>0)) {
				common.showTip({
					title : localeMsg.common.warn,
					text : localeMsg.common.selectMsg
				});
			}
			else
			main.unmapping(_groupId, lunIdArr);
		},
		/**
		 * 解除映射
		 * @param groupId
		 * @param lunArr
		 */
		unmapping : function(groupId, lunArr) {
			$.ajax({
				type : "post",
				url : baseURL + "/mapping/delete",
				traditional: true,
				data : {groupId: groupId, ids : lunArr},
				dataType : "json",
				success : function(json) {
					common.showTip({
						title : localeMsg.common.prompt,
						text : localeMsg.common.saveSuccess,
						success : true,
						callback : function() {main.showMappings(groupId);}
					});
				}
			});
		},
		
};